package com.project.androidjukebox;
 
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
/*
 * Class that will sort out all mp3 files from directory
 */
class Mp3Filter implements FilenameFilter{
	@Override
	public boolean accept(File dir, String name) {
		return (name.endsWith(".mp3"));
	}
}


public class BarTab extends ListActivity {
	
	 /**
     * Variables
     */
    MediaPlayer mp = new MediaPlayer();		// MediaPlayer instance which will do the playback
    public static ArrayList<HashMap<String, String>> songs = new ArrayList<HashMap<String, String>>();		// List of songs on the PATH directory
    private static final String SD_PATH = new String("/sdcard/Jukebox/");		// Path were the songs that you want are in you mobile device
	Server s = new Server();
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bar_layout);
        
        updatePlayList();    
        
        // Stopping the playback
        Button stop = (Button) findViewById(R.id.stop);
        stop.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mp.stop();
        }                    
    });
        
        Button refresh = (Button) findViewById(R.id.refresh);
        refresh.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
               	setListAdapter(getListAdapter());
        }  
        });
    } 
    
    /*
     * Method for getting the latest mp3 files from directory 
     * and adding them to ArrayList which will be shown in ListVIew   
     */
    public void updatePlayList(){
    	//Clearing last list
    	songs.clear();
    	//Finding all mp3 files
    	File home = new File(SD_PATH);
    	if(home.listFiles( new Mp3Filter()).length > 0){
    		
    		for (File file : home.listFiles( new Mp3Filter())){
    			HashMap<String, String> map = new HashMap<String, String>();
    			String fileName = file.getName();
    			map.put("Name", fileName.substring(0, fileName.length()-4)); // subtracts the ".mp3" ending
    			map.put("Votes", "0");
    			map.put("Path", fileName);
    			songs.add(map);
    		}
    		//Send to "server"
    		s.setList(songs);

    		setListAdapter(getListAdapter());
    	}
    }
    
    public ListAdapter getListAdapter(){
    	ListAdapter adapter = new SimpleAdapter(BarTab.this, s.getList(),
				R.layout.playbacklistitem,
				new String[] { "Name", "Votes" }, new int[] {
						R.id.title, R.id.votes});
    	return adapter;
    }
    
    //This method will start the playback if user clicks on the list
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
	    try{
	    	mp.reset();
	    	mp.setDataSource(SD_PATH + songs.get(position).get("Path"));
	    	Log.v("THIS IS THE PATH", SD_PATH + songs.get(position).get("Path"));
	    	mp.prepare();
	    	mp.start();
	    }catch(IOException e){
	    	Log.v(getString(R.string.app_name), e.getMessage());
	    }
    }

}
